
//--> Loth 2011 
//--> Basic : only Interface & flash 10 api

package chamans.engine
{
	import chamans.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.ui.*;
	import flash.utils.Timer;
	
	public class Basic extends Sprite
	{
		public static var stage:Stage;
		public var FlashLimiteVersion:Array=[10, 0, 0, 0];
		public var physicEngine:String;
		public var Engine:String;
		// chamans
		public var LOAD:load;
		public var TOOL:Tool;
		public var KEY:key;
		public var SYS:Interface;
		public var PAUSE:PauseScreen;
		// init variable
		private var END:Function;
		private var LOOP:Function;
		private var def:Object;
		// basic variable	
		public var autoMove:Boolean=false;
		public var mouseHide:Boolean=false;
		public var MenuActif:Boolean=false;
		
		public var limite:Boolean=true;
		public var size:Point=new Point(0,0);
		public var sizelimit:Point=new Point(800,450);
		// 3d in flash 10
		public var content:Sprite;
		public var back:Sprite;
		public var view:Sprite;
		public var front:Sprite;
		public var V:PerspectiveProjection;
		public var Viewlimite:Vector3D = new Vector3D(40,10,0);
		public var viewaxis:Point;
		public var move:Boolean=false;
		
		private function OO(event:Event=null):void
		{
			if(move)CamMove();
		}
		public function Basic(end:Function=null, loop:Function=null, setting:Object=null)
		{
			Engine="Basic";
			END=end;
			LOOP=loop;
			def = setting || new Object();
			
			if(stage) init(); //--> flash builder
			else addEventListener(Event.ADDED_TO_STAGE,onStage);//--> flash cs5
		}
		private function onStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onStage);
			init();
		}
		private function init():void
		{
			// chamans usefull utils
			LOAD = new load(); TOOL = new Tool(); KEY = new key(stage);
			// stage set
			with (stage){ align = "TL"; scaleMode = "noScale"; quality = "HIGH"; }
			init_2D();
		}
		//________________________________Interface 2D init
		private function init_2D():void
		{
			SYS = new Interface( this, init_VIEW3D, def);
			addChild(SYS);
		}
		// ______________________________ flash api 10 3d init
		private function init_VIEW3D():void
		{
			viewaxis=new Point();
			content = new Sprite();
			back = new Sprite();
			view = new Sprite();
			front = new Sprite();
			addChildAt(content,0);
			content.addChild(back);
			content.addChild(view);
			content.addChild(front);
			RESIZE();
			SYS.OUT(SYS.MESSAGE);
			Active();
			See();
			if(END!=null)END();
		}
		//________________________________________resize
		public function RESIZE(e:Event=null):void
		{
			V = this.transform.perspectiveProjection;
			V.fieldOfView = 120;
			size=new Point( stage.stageWidth, stage.stageHeight);
			SYS.OUT("resize "+size.x+" "+size.y);
			if(SYS.actif)SYS.cadre(size);
			if(PAUSE) PAUSE.Resize();
			if(view) RESIZE3D();
			if(limite && size.x==sizelimit.x && size.y==sizelimit.y)activeResize(false);
			
		}
		public function RESIZE3D(e:Event=null):void
		{
			content.x=size.x>>1;
			content.y=size.y>>1;
			V.projectionCenter=new Point(size.x>>1,size.y>>1);
		}
		//__________________________________________ Pause screen
		private function Active(e:Event=null):void
		{
			stage.removeEventListener(Event.ACTIVATE,Active);
			stage.addEventListener(Event.ENTER_FRAME, OO, false, 0, true);
			stage.addEventListener(Event.DEACTIVATE,Desactive, false, 0, true);
			if(PAUSE!=null){PAUSE.clear(); removeChild(PAUSE);PAUSE=null};
		}
		private function Desactive(e:Event):void 
		{
			stage.removeEventListener(Event.ENTER_FRAME, OO);
			stage.removeEventListener(Event.DEACTIVATE,Desactive);
			stage.addEventListener(Event.ACTIVATE,Active, false, 0, true);
			if(PAUSE==null) PAUSE=new PauseScreen(this); addChild(PAUSE);
		}
		public function activeResize(n:Boolean=true):void 
		{
			if(n)stage.addEventListener('resize',  RESIZE, false, 0, true);
			else {stage.removeEventListener('resize',  RESIZE); 
				SYS.OUT(SYS.MESSAGE);
		}}
		//__________________________________________special rotation view
		public function Delete(Clip:*):void{ while (Clip.numChildren)Clip.removeChildAt(0);}
		private function See():void
		{ 
			stage.addEventListener(MouseEvent.MOUSE_MOVE, ActiveView, false, 0, true);
			stage.addEventListener(Event.MOUSE_LEAVE,DesactiveView, false, 0, true)
		}
		private function ActiveView(e:Event):void
		{
			move = true;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,ActiveView);
		}
		private function DesactiveView(e:Event):void
		{
			move = false;
			TweenNano.to(view, .5, { rotationY:0, rotationX:0,  onComplete:See});
		}
		//_____________________________________________ camera simulation
		private function CamMove(e:Event=null):void
		{
			viewaxis.x=(-(Viewlimite.x>>1) + (Viewlimite.x*(mouseX / stage.stageWidth)))
			viewaxis.y=((Viewlimite.y>>1) - (Viewlimite.y*(mouseY / stage.stageHeight)))
			TweenNano.to(view, .3, { rotationY:-viewaxis.x, rotationX:-viewaxis.y, ease:Expo.easeOut });
		}
		//_______________________________________________time pause
		public function Pause ( END:Function ):void
		{
			tmpFunction=END;
			time = new Timer(delay, 1 );
			time.start();
			time.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
		}
		private function completeHandler(e:TimerEvent):void
		{
			time.removeEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
			tmpFunction();
			time.stop();
		}
		private var time:Timer;
		private var delay:uint = 1000;//1000 = 1 seconde
		private var tmpFunction:Function;
		//END
	}}